package com.ys.nms.module.business.controller.admin.ports;

import com.baomidou.dynamic.datasource.annotation.Slave;
import com.ys.nms.framework.tenant.core.aop.TenantIgnore;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import static com.ys.nms.framework.common.pojo.CommonResult.success;

import com.ys.nms.framework.excel.core.util.ExcelUtils;

import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;
import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ys.nms.module.business.controller.admin.ports.vo.*;
import com.ys.nms.module.business.dal.dataobject.ports.PortsDO;
import com.ys.nms.module.business.service.ports.PortsService;

@Tag(name = "管理后台 - 端口")
@RestController
@RequestMapping("/business/ports")
@Validated
public class PortsController {

    @Resource
    private PortsService portsService;

    @PostMapping("/create")
    @Operation(summary = "创建端口")
    @PreAuthorize("@ss.hasPermission('business:ports:create')")
    public CommonResult<Integer> createPorts(@Valid @RequestBody PortsSaveReqVO createReqVO) {
        return success(portsService.createPorts(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新端口")
    @PreAuthorize("@ss.hasPermission('business:ports:update')")
    public CommonResult<Boolean> updatePorts(@Valid @RequestBody PortsSaveReqVO updateReqVO) {
        portsService.updatePorts(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除端口")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:ports:delete')")
    public CommonResult<Boolean> deletePorts(@RequestParam("id") Integer id) {
        portsService.deletePorts(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除端口")
                @PreAuthorize("@ss.hasPermission('business:ports:delete')")
    public CommonResult<Boolean> deletePortsList(@RequestParam("ids") List<Integer> ids) {
        portsService.deletePortsListByIds(ids);
        return success(true);
    }

    @Slave
    @GetMapping("/get")
    @Operation(summary = "获得端口")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:ports:query')")
    public CommonResult<PortsRespVO> getPorts(@RequestParam("id") Integer id) {
        PortsDO ports = portsService.getPorts(id);
        return success(BeanUtils.toBean(ports, PortsRespVO.class));
    }

    @Slave
    @GetMapping("/page")
    @Operation(summary = "获得端口分页")
    @PreAuthorize("@ss.hasPermission('business:ports:query')")
    public CommonResult<PageResult<PortsRespVO>> getPortsPage(@Valid PortsPageReqVO pageReqVO) {
        PageResult<PortsDO> pageResult = portsService.getPortsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PortsRespVO.class));
    }

    @TenantIgnore
    @Slave
    @GetMapping("/list")
    @Operation(summary = "获得端口分页")
    @PreAuthorize("@ss.hasPermission('business:ports:query')")
    public CommonResult<List<PortsRespVO>> getPortsList(@Valid PortsPageReqVO pageReqVO) {
        List<PortsDO> pageResult = portsService.getPortsListByDevicesId(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PortsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出端口 Excel")
    @PreAuthorize("@ss.hasPermission('business:ports:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPortsExcel(@Valid PortsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PortsDO> list = portsService.getPortsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "端口.xls", "数据", PortsRespVO.class,
                        BeanUtils.toBean(list, PortsRespVO.class));
    }

}